﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WoobaoForms
{
    public partial class FrmStatus : Form
    {
        public FrmStatus()
        {
            InitializeComponent();
        }

        private void btnCadastrar_Click(object sender, EventArgs e)
        {
            using (DBWoobaoEntities banco = new DBWoobaoEntities())
            {
                TBStatu s = new TBStatu();

                s.DEStatus = this.txtStatus.Text;
                banco.TBStatus.Add(s);
                banco.SaveChanges();

            }
            MessageBox.Show("Status Cadastrado com Sucesso!!");

            txtStatus.Text = "";
        }

        private void btnPesquisar_Click(object sender, EventArgs e)
        {
            using (DBWoobaoEntities banco = new DBWoobaoEntities())
            {
                var list = from usu in banco.TBStatus
                           where
                           usu.DEStatus.Contains(txtStatus.Text)
                           select usu;
                dgvStatus.DataSource = list.ToList();
                cboStatus.DataSource = banco.TBStatus.ToList();
            }
        }

        private void btnExcluir_Click(object sender, EventArgs e)
        {
            using (DBWoobaoEntities banco = new DBWoobaoEntities())
            {
                int codigo = (int)cboStatus.SelectedValue;

                TBStatu s = banco.TBStatus.Where(sta => sta.IDStatus == codigo).Single();

                banco.TBStatus.Remove(s);
                banco.SaveChanges();
                cboStatus.DataSource = banco.TBStatus.ToList();
                dgvStatus.DataSource = banco.TBStatus.ToList();

            }
            MessageBox.Show("Status excluido com Sucesso!!");

        }

        private void cboStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtStatus.Text = txtStatus.Text;

        }

        private void btnAtualizar_Click(object sender, EventArgs e)
        {
            using (DBWoobaoEntities banco = new DBWoobaoEntities())
            {
                int codigo = (int)cboStatus.SelectedValue;

                TBStatu s = banco.TBStatus.Where(sta => sta.IDStatus == codigo).Single();

                s.DEStatus = txtStatus.Text;
                banco.SaveChanges();

                cboStatus.DataSource = banco.TBStatus.ToList();
                dgvStatus.DataSource = banco.TBStatus.ToList();

            }
            MessageBox.Show("Status Atualizado com Sucesso!!");
        }
    }
}